<?php

namespace App\Http\Controllers\Coms;

use App\Http\Controllers\Admin\AdminController;
use App\Http\Controllers\Controller;
use App\Utils\GDUtil;
use DB;
use ReflectionClass;


/**
 * @contro 通用功能控制器
 */
class ComsController extends Controller
{

    /**
     * curl模拟post请求, 测试接口数据
     * @return mixed
     */
    public function apiTest()
    {
        $base_url = $_SERVER['HTTP_HOST'] . "/Laravel-Shop/trunk/public/api/";

        $data = [
            'cate_id' => 1,
            'goods_id' => '2,3',
        ];
        $curl = curl_init();
        //设置选项，包括URL
        curl_setopt($curl, CURLOPT_URL, $base_url . 'root/auth/floor_goods');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        //执行并获取HTML文档内容
        $output = curl_exec($curl);

        //释放curl句柄
        curl_close($curl);
        return $output;
    }

    /**
     * 存储后台导航菜单当前位置信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function storeMenu(Request $request)
    {
        if ($request->menu == '') {
            $this->data['menu'] = ComUtil::$menu;
        } else {
            ss([
                'menu' => json_decode($request->menu),
            ]);
            $this->data['menu'] = ss('menu');
        }

        return $this->apiJson();
    }

    /**
     * 后台通用验证码
     */
    public function adminCode()
    {
        $code = GDUtil::makeCode(5);
        ss([GDUtil::YZM_ADMIN_CODE => $code['code']]);
        imagepng($code['img']);
        imagedestroy($code['img']);
    }

    /**
     * @route coms/role_act
     * @auth 权限管理
     * @desc 是否拥有权限
     */
    public function roleAct(Request $request)
    {
        //权限控制器
        $controllerArr = [
            AdminController::class,
            ComsController::class
        ];

        $controRoles = array();
        foreach ($controllerArr as $controller) {
            //权限 描述 控制器名称
            $class = new ReflectionClass($controller);
            preg_match_all('/@contro(.*?)\n/', $class->getDocComment(), $contro_arr);
            $controDesc = $contro_arr[1] ? trim($contro_arr[1][0]) : '';


            $methods = $class->getMethods();
            $propName = array();

            foreach ($class->getParentClass()->getMethods() as $prop) {
                $propName[] = $prop->name;
            }

            $routeConMeth = array();
            foreach ($methods as $method) {
                if (!in_array($method->name, $propName)) {
                    $doc = $method->getDocComment();    //获取注释块
                    preg_match_all('/@route(.*?)\n/', $doc, $route_arr);  //获取权限名称数组
                    preg_match_all('/@auth(.*?)\n/', $doc, $auth_arr);  //获取权限名称数组
                    preg_match_all('/@desc(.*?)\n/', $doc, $desc_arr);  //

                    $meth['route'] = $route_arr[1] ? trim($route_arr[1][0]) : '';
                    $meth['auth'] = $auth_arr[1] ? trim($auth_arr[1][0]) : '';
                    $meth['desc'] = $desc_arr[1] ? trim($desc_arr[1][0]) : '';
                    $routeConMeth[] = $meth;
                }

            }

            $contro['desc'] = $controDesc;
            $contro['method'] = $routeConMeth;
            $controName = array_last(explode('\\', $controller));
            $controRoles[$controName] = $contro;
        }
        return $controRoles;
    }

}